﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.SessionState;

namespace Frame
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "ViewSetting&Show&Action",
                "{controller}/{action}/{ViewForder}/{ViewName}/{KeyA}/{KeyB}/{KeyC}/{KeyD}",
                new { KeyA = UrlParameter.Optional, KeyB = UrlParameter.Optional, KeyC = UrlParameter.Optional, KeyD = UrlParameter.Optional }
            );

            routes.MapRoute(
                "Default",
                "{controller}/{action}/",
                new { controller = "Frame", action = "Login", id = UrlParameter.Optional }
            ); 
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            Application["SessionDictionary"] = new Dictionary<string, Dictionary<string, object>>();
        }

        protected void Session_Start()
        {
            Application.Lock();
            Dictionary<string, Dictionary<string, object>> S = (Dictionary<string, Dictionary<string, object>>)Application["SessionDictionary"];
            if (!S.ContainsKey(Session.SessionID))
                S.Add(Session.SessionID, new Dictionary<string, object>());
            Application.UnLock();
        }

        protected void Session_End()
        {
            Application.Lock();
            ((Dictionary<string, Dictionary<string, object>>)Application["SessionDictionary"]).Remove(Session.SessionID);
            Application.UnLock();
        }
    }
}